﻿using System;
using COM.DTO;
using NHibernate;
using COM.Logging;
using NHibernate.Criterion;

namespace DAL.DAO
{
    class DetailTypeDAO
    {
        public static void Add(DetailType detailType)
        {
            {
                using (ISession session = NHibernateHelper.OpenSession())
                using (ITransaction transaction = session.BeginTransaction())
                {
                    session.SaveOrUpdate(detailType);
                    transaction.Commit();
                }
            }
        }
        public static void Update(DetailType detailType)
        {
            {
                using (ISession session = NHibernateHelper.OpenSession())
                using (ITransaction transaction = session.BeginTransaction())
                {
                    session.Update(detailType);
                    transaction.Commit();
                }
            }
        }

        public static void Remove(DetailType detailType)
        {
            {
                using (ISession session = NHibernateHelper.OpenSession())
                using (ITransaction transaction = session.BeginTransaction())
                {
                    session.Delete(detailType);
                    transaction.Commit();
                }
            }
        }

        public static DetailType GetDetailTypeById(String detailTypeId)
        {
            using (ISession session = NHibernateHelper.OpenSession())
                return session.Get<DetailType>(detailTypeId);
        }
        public static DetailType GetDetailTypeByName(string detailTypeName)
        {
            using (ISession session = NHibernateHelper.OpenSession())
            {
                var detailType = session
                    .CreateCriteria(typeof(DetailType))
                    .Add(Restrictions.Eq("DetailTypeName", detailTypeName))
                    .UniqueResult<DetailType>();
                return detailType;
            }

        }
    }
}
